package com.o2opays.wechat.service.Impl;

import com.o2opays.wechat.Enum.EnApi;
import com.o2opays.wechat.pojo.HttpResult;
import com.o2opays.wechat.pojo.MerchatUserInfo;
import com.o2opays.wechat.service.LoginService;
import com.o2opays.wechat.tool.HttpClientUtil;
import com.o2opays.wechat.tool.JsonUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by tugou on 2017/12/3.
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Value("${service_url}")
    private String service_url;

    @Override
    public HttpResult login(String openId) {
        String url = service_url + EnApi.getUserInfoByOpenId;
        Map<String,String> par = new HashMap<String,String>();
        par.put("openId",openId);
        return login(url,par);
    }

    @Override
    public HttpResult login(String openId, String pwd, String userName) {
        String url = service_url + EnApi.getUserInfoByPwd;
        Map<String,String> par = new HashMap<String,String>();
        par.put("openId",openId);
        par.put("pwd",pwd);
        par.put("userName",userName);
        return login(url,par);
    }

    @Override
    public HttpResult loginOut(String openId) {
        String url = service_url + EnApi.loginOut;
        Map<String,String> par = new HashMap<String,String>();
        par.put("openId",openId);
        String result = HttpClientUtil.doGet(url,par);
        HttpResult httpResult = JsonUtil.toObject(result,HttpResult.class);
        return httpResult;
    }

    private HttpResult login(String url,Map par){
        String result = HttpClientUtil.doGet(url,par);
        HttpResult httpResult = JsonUtil.toObject(result,HttpResult.class);
        MerchatUserInfo info = JsonUtil.toObject(JsonUtil.toJson(httpResult.getData()),MerchatUserInfo.class);
        httpResult.data = info;
        return httpResult;
    }
}
